package com.xunge.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xunge.mapper.AttendanceMapper;
import com.xunge.pojo.Attendance;
import com.xunge.pojo.AttendanceQueryParam;
import com.xunge.pojo.PageResult;
import com.xunge.service.AttendanceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class AttendanceServiceImpl implements AttendanceService {

    @Autowired
    private AttendanceMapper attendanceMapper;

    /**
     * 分页查询考勤信息
     */
    @Override
    public PageResult<Attendance> page(AttendanceQueryParam attendanceQueryParam) {
        // 设置分页参数
        PageHelper.startPage(attendanceQueryParam.getPage(), attendanceQueryParam.getPageSize());
        // 执行查询
        Page<Attendance> page = (Page<Attendance>) attendanceMapper.list(attendanceQueryParam);
        // 封装分页结果
        return new PageResult<>(page.getTotal(), page.getResult());
    }

    /**
     * 新增考勤记录
     */
    @Override
    public void save(Attendance attendance) {
        attendance.setCreateTime(LocalDateTime.now());
        attendance.setUpdateTime(LocalDateTime.now());
        attendanceMapper.insert(attendance);
    }

    /**
     * 根据ID查询考勤记录
     */
    @Override
    public Attendance getById(Integer id) {
        return attendanceMapper.getById(id);
    }

    /**
     * 根据ID修改考勤记录
     */
    @Override
    public void update(Attendance attendance) {
        attendance.setUpdateTime(LocalDateTime.now());
        attendanceMapper.update(attendance);
    }

    /**
     * 根据ID删除考勤记录
     */
    @Override
    public void deleteById(Integer id) {
        attendanceMapper.deleteById(id);
    }

    /**
     * 批量删除考勤记录
     */
    @Override
    public void delete(List<Integer> ids) {
        attendanceMapper.deleteByIds(ids);
    }

    /**
     * 根据学生ID查询考勤记录
     */
    @Override
    public List<Attendance> getByStudentId(Integer studentId) {
        return attendanceMapper.getByStudentId(studentId);
    }

    /**
     * 根据课程ID查询考勤记录
     */
    @Override
    public List<Attendance> getByCourseId(Integer courseId) {
        return attendanceMapper.getByCourseId(courseId);
    }

    /**
     * 根据班级ID查询考勤记录
     */
    @Override
    public List<Attendance> getByClazzId(Integer clazzId) {
        return attendanceMapper.getByClazzId(clazzId);
    }
} 